﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class F520 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string v_str_gdID = Request.QueryString["gdID"];
            int v_int_gdID = 0;
            int.TryParse(v_str_gdID, out v_int_gdID);
            if (v_int_gdID != 0)
            {
                this.m_cmd_tao_moi.Enabled = false;
                this.m_cmd_cap_nhat.Enabled = true;
            }
            else
            {
                this.m_cmd_tao_moi.Enabled = true;
                this.m_cmd_cap_nhat.Enabled = false;
            }
            LoadSanPham();
            BindData();
        }
    }

    private void LoadSanPham()
    {
        string v_str_monhocID = Request.QueryString["monhocId"];
        int v_int_monhocID = 0;
        int.TryParse(v_str_monhocID, out v_int_monhocID);
        cboSanPham.DataSource = Services.GetSanPhamByMonHoc(v_int_monhocID);
        cboSanPham.DataTextField = "TEN_SAN_PHAM";
        cboSanPham.DataValueField = "ID";
        cboSanPham.DataBind();
    }

    private void LoadPhienBan()
    {
        string v_str_monhocID = Request.QueryString["monhocId"];
        int v_int_monhocID = 0;
        int.TryParse(v_str_monhocID, out v_int_monhocID);

        string v_str_sanphamID = cboSanPham.SelectedValue;
        int v_int_sanphamID = 0;
        int.TryParse(v_str_sanphamID, out v_int_sanphamID);

        cboPhienBan.DataSource = Services.GetPhienBanByMonHocSanPham(v_int_monhocID, v_int_sanphamID);
        cboPhienBan.DataTextField = "TEN_VERSION";
        cboPhienBan.DataValueField = "ID";
        cboPhienBan.DataBind();
    }

    private void BindData()
    {
        try
        {
            DM_MON_HOC monhoc = new DM_MON_HOC();
            m_lbl_thong_bao.Text = "Không có thông báo gì.";

            string v_str_donviID = Request.QueryString["donviId"];
            int v_int_donviID = 0;
            int.TryParse(v_str_donviID, out v_int_donviID);

            string v_str_nganhID = Request.QueryString["nganhId"];
            int v_int_nganhID = 0;
            int.TryParse(v_str_nganhID, out v_int_nganhID);

            string v_str_monhocID = Request.QueryString["monhocId"];
            int v_int_monhocID = 0;
            int.TryParse(v_str_monhocID, out v_int_monhocID);

            //BinData for update
            string v_str_gdID = Request.QueryString["gdID"];
            int v_int_gdID = 0;
            int.TryParse(v_str_gdID, out v_int_gdID);
            GD_SAN_PHAM_DON_VI gd_san_pham_dv = Services.GetDetailGDSanPham(v_int_gdID);
            if (gd_san_pham_dv != null)
            {
                ListItem itemSelect = this.cboSanPham.Items.FindByValue(gd_san_pham_dv.ID_SAN_PHAM.ToString());
                if (itemSelect != null)
                {
                    int index = this.cboSanPham.Items.IndexOf(itemSelect);
                    this.cboSanPham.SelectedIndex = index;
                    LoadPhienBan();
                }

                ListItem itemPhienBan = this.cboPhienBan.Items.FindByValue(gd_san_pham_dv.ID_VERSION.ToString());
                if (itemPhienBan != null)
                {
                    int index = this.cboPhienBan.Items.IndexOf(itemPhienBan);
                    this.cboPhienBan.SelectedIndex = index;
                }

                this.txtNgayCap.Text = gd_san_pham_dv.NGAY_CAP.ToString("yyyy-MM-dd");
                this.txtGhiChu.Text = gd_san_pham_dv.GHI_CHU;
            }


            if ((v_int_donviID != 0) && (v_int_nganhID != 0) && (v_int_monhocID != 0))
            {
                monhoc = Services.GetDM_MON_HOCById(v_int_monhocID);
                this.lblMonHocInfo.Text = !String.IsNullOrEmpty(monhoc.TEN_TIENG_VIET) ? "Môn học: " + monhoc.TEN_TIENG_VIET : "Không xác định được tên môn học";
            }

            this.lblF221.Text = !String.IsNullOrEmpty(monhoc.TEN_TIENG_VIET) ? "F221 - Thêm mới sản phẩm - " + monhoc.TEN_TIENG_VIET : "F221 - Thêm mới sản phẩm";

            List<GetSanPhamDonViResult> lst = Services.GetGDPhienBan(v_int_donviID, v_int_nganhID, v_int_monhocID);

            if (lst != null)
            {
                gvVersion.DataSource = lst;
                gvVersion.DataBind();
                // Set event for Delete button
                foreach (GridViewRow row in gvVersion.Rows)
                {
                    ImageButton imageButton = (ImageButton)row.FindControl("imgBtDelete");
                    imageButton.Attributes.Add("OnClick", "javascript:return confirm('Bạn có chắc chắn muốn xóa sản phẩm này?')");
                }
            }
            else
            {

            }

        }
        catch (Exception)
        {

        }

    }

    protected void gvVersion_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string v_str_donviID = Request.QueryString["donviId"];
        int v_int_donviID = 0;
        int.TryParse(v_str_donviID, out v_int_donviID);

        string v_str_nganhID = Request.QueryString["nganhId"];
        int v_int_nganhID = 0;
        int.TryParse(v_str_nganhID, out v_int_nganhID);

        string v_str_monhocID = Request.QueryString["monhocId"];
        int v_int_monhocID = 0;
        int.TryParse(v_str_monhocID, out v_int_monhocID);

        int id = Convert.ToInt32(e.CommandArgument);

        switch (e.CommandName.ToUpper())
        {
            case "EDIT":
                Response.Redirect(String.Format("F520.aspx?donviId={0}&nganhId={1}&monhocId={2}&gdID={3}", v_int_donviID, v_int_nganhID, v_int_monhocID, id));
                break;
            case "DEL":
                GD_SAN_PHAM_DON_VI v_obj_gd_san_pham_don_vi = Services.GetDetailGDSanPham(id);
                bool isok = Services.DeleteGDSanPham(id);
                if (isok)
                {
                    BindData();
                    this.m_lbl_mess.Text = "Xóa bản ghi thành công!";
                    m_lbl_mess.Visible = true;
                }
                else
                {
                    this.m_lbl_mess.Text = "Lỗi, xóa không thành công!";
                    m_lbl_mess.Visible = true;
                }
                break;
        }
    }

    protected void m_cmd_tao_moi_Click(object sender, EventArgs e)
    {
        try
        {
            string v_str_donviID = Request.QueryString["donviId"];
            int v_int_donviID = 0;
            int.TryParse(v_str_donviID, out v_int_donviID);

            string v_str_nganhID = Request.QueryString["nganhId"];
            int v_int_nganhID = 0;
            int.TryParse(v_str_nganhID, out v_int_nganhID);

            string v_str_monhocID = Request.QueryString["monhocId"];
            int v_int_monhocID = 0;
            int.TryParse(v_str_monhocID, out v_int_monhocID);

            string v_str_sanphamID = cboSanPham.SelectedValue;
            int v_int_sanphamID = 0;
            int.TryParse(v_str_sanphamID, out v_int_sanphamID);

            string v_str_phienbanID = cboPhienBan.SelectedValue;
            int v_int_phienbanID = 0;
            int.TryParse(v_str_phienbanID, out v_int_phienbanID);

            GD_SAN_PHAM_DON_VI gdSanPham = new GD_SAN_PHAM_DON_VI();
            gdSanPham.ID_DON_VI_NGANH_MON_HOC = Services.GetDM_DON_VI_NGANH_MON_HOC(v_int_donviID, v_int_nganhID, v_int_monhocID).ID;
            gdSanPham.ID_MON_HOC = v_int_monhocID;
            gdSanPham.ID_SAN_PHAM = v_int_sanphamID;
            gdSanPham.ID_VERSION = v_int_phienbanID;
            gdSanPham.NGAY_CAP = DateTime.ParseExact(this.txtNgayCap.Text, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture); 
            gdSanPham.GHI_CHU = txtGhiChu.Text;
            bool isOk = Services.InsertGDSanPham(gdSanPham);
            if (isOk)
            {
                ClearForm();
                BindData();
                this.m_lbl_mess.Text = "Thêm mới phiên bản thành công!";
                m_lbl_mess.Visible = true;
            }
            else {
                this.m_lbl_mess.Text = "Lỗi khi thêm mới phiên bản!";
                m_lbl_mess.Visible = true;
            }
        }
        catch (Exception)
        {

        }

    }
    private void ClearForm()
    {
        txtGhiChu.Text = "";
        txtNgayCap.Text = "";
    }
    protected void cboSanPham_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadPhienBan();
    }
    protected void m_cmd_cap_nhat_Click(object sender, EventArgs e)
    {
        string v_str_gdID = Request.QueryString["gdID"];
        int v_int_gdID = 0;
        int.TryParse(v_str_gdID, out v_int_gdID);
        if (v_int_gdID != 0)
        {
            try
            {
                string v_str_donviID = Request.QueryString["donviId"];
                int v_int_donviID = 0;
                int.TryParse(v_str_donviID, out v_int_donviID);

                string v_str_nganhID = Request.QueryString["nganhId"];
                int v_int_nganhID = 0;
                int.TryParse(v_str_nganhID, out v_int_nganhID);

                string v_str_monhocID = Request.QueryString["monhocId"];
                int v_int_monhocID = 0;
                int.TryParse(v_str_monhocID, out v_int_monhocID);

                string v_str_sanphamID = cboSanPham.SelectedValue;
                int v_int_sanphamID = 0;
                int.TryParse(v_str_sanphamID, out v_int_sanphamID);

                string v_str_phienbanID = cboPhienBan.SelectedValue;
                int v_int_phienbanID = 0;
                int.TryParse(v_str_phienbanID, out v_int_phienbanID);

                GD_SAN_PHAM_DON_VI gdSanPham = new GD_SAN_PHAM_DON_VI();
                gdSanPham.ID = v_int_gdID;
                gdSanPham.ID_DON_VI_NGANH_MON_HOC = Services.GetDM_DON_VI_NGANH_MON_HOC(v_int_donviID, v_int_nganhID, v_int_monhocID).ID;
                gdSanPham.ID_MON_HOC = v_int_monhocID;
                gdSanPham.ID_SAN_PHAM = v_int_sanphamID;
                gdSanPham.ID_VERSION = v_int_phienbanID;
                gdSanPham.NGAY_CAP = DateTime.ParseExact(this.txtNgayCap.Text, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture); 
                gdSanPham.GHI_CHU = txtGhiChu.Text;
                bool isOk = Services.UpdateGDSanPham(gdSanPham);
                if (isOk)
                {
                    BindData();
                    this.m_lbl_mess.Text = "Cập nhật phiên bản thành công!";
                    m_lbl_mess.Visible = true;
                }
                else {
                    this.m_lbl_mess.Text = "Lỗi khi cập nhật phiên bản!";
                    m_lbl_mess.Visible = true;
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
            }
        }
        else
        {
            this.m_lbl_mess.Text = "Lỗi!";
            m_lbl_mess.Visible = true;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string v_str_donviID = Request.QueryString["donviId"];
        int v_int_donviID = 0;
        int.TryParse(v_str_donviID, out v_int_donviID);

        string v_str_nganhID = Request.QueryString["nganhId"];
        int v_int_nganhID = 0;
        int.TryParse(v_str_nganhID, out v_int_nganhID);

        string v_str_monhocID = Request.QueryString["monhocId"];
        int v_int_monhocID = 0;
        int.TryParse(v_str_monhocID, out v_int_monhocID);
        Response.Redirect(String.Format("~/F520.aspx?donviId={0}&nganhId={1}&monhocId={2}", v_int_donviID, v_int_nganhID, v_int_monhocID));
    }
    protected void gvVersion_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvVersion.PageIndex = e.NewPageIndex;
        BindData();
    }
}